/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DBTables;

import DBManager.*;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Иван
 */
public class TUser extends DataManager {

    public String Login;
    public String Password;
    public String E_Mail;
    public String Interests;

    public TUser(Connection conn, String tableName) {
        super(conn, tableName);

        try {
            SelectPrepStatement = connection.prepareStatement("select * from " + TableName);
            InsertPrepStatement = connection.prepareStatement("insert into " + TableName + " +(Id_User, Login,Password,E_Mail,Interests) values ('3',?,?,?,?) ");
            UpdatePrepStatement = connection.prepareStatement("update " + TableName + " set Login = ?,Password = ?,E_Mail = ?,Interests = ? Where Id_User = ?");
            DeletePrepStatement = connection.prepareStatement("delete from " + TableName + " where Id_User = ?");
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error database access! Possible connection is closed! ");
        }

    }

    @Override
    public void SetValues(PreparedStatement prepareStatement) {
        try {
            ParameterMetaData metaData = prepareStatement.getParameterMetaData();
            int countParameters = metaData.getParameterCount();

            if (countParameters >= 4) // for insert prepared statement
            {
                prepareStatement.setString(1, Login);
                prepareStatement.setString(2, Password);
                prepareStatement.setString(3, E_Mail);
                prepareStatement.setString(4, Interests);
            }

            if ((countParameters == 5))// for update prepared statement
            {
                prepareStatement.setString(5, ID_Table);
            }

            if ((countParameters == 1))// for delete prepared statement
            {
                prepareStatement.setString(1, ID_Table);
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error database access!");
        }


    }
}
